<template>
    <div class="article-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="供货商名称">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="联系人姓名">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.nickname"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select class="w-[280px]" v-model="queryParams.isDisable">
                        <el-option label="全部" value />
                        <el-option label="启用" :value="0" />
                        <el-option label="禁用" :value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.mobile"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <router-link v-perms="['article:add', 'article:add/edit']" to="/suppliers/edit">
                    <el-button type="primary" class="mb-4">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增供货商
                    </el-button>
                </router-link>
            </div>
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="编号" prop="id" min-width="80" />
                <el-table-column label="供应商名称" min-width="100" prop="name"></el-table-column>
                <el-table-column label="状态" min-width="100">
                    <template #default="{ row }">
                        <el-switch
                            v-perms="['article:cate:change']"
                            v-model="row.isDisable"
                            :active-value="0"
                            :inactive-value="1"
                            @change="changeStatus(row.id)"
                        />
                    </template>
                </el-table-column>

                <el-table-column label="供货商类型" min-width="80">
                    <template #default="{ row }">
                        <span v-if="row.supplierType == 1">积分供货商</span>
                        <span type="danger" v-else>现金供货商</span>
                    </template>
                </el-table-column>
                <el-table-column label="联系人姓名" prop="nickname" min-width="120" />
                <el-table-column label="联系人手机号" prop="mobile" min-width="120" />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['article:edit', 'article:add/edit']"
                            type="primary"
                            link
                        >
                            <router-link
                                :to="{
                                    path: '/suppliers/edit',
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >
                                编辑
                            </router-link>
                        </el-button>
                        <el-button
                            v-perms="['article:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="articleLists">
import { usePaging } from '@/hooks/usePaging'
import { del8, disable, listPage } from '@/servers/user/api/supplyChaingonghuoshangguanli'
import feedback from '@/utils/feedback'
const queryParams = reactive({
    name: undefined,
    mobile: undefined,
    isDisable: undefined,
    nickname: undefined
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: listPage,
    params: queryParams
})

const changeStatus = async (id: number) => {
    try {
        await disable({ id })
        feedback.msgSuccess('修改成功')
        getLists()
    } catch (error) {
        getLists()
    }
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await del8({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

onActivated(() => {
    getLists()
})

getLists()
</script>
